//------------------------------------------------------------------------------
//  <copyright file="GeometryElementGroup3D.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.Robotics.Geometry
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// A class that holds a group of geometry elements.
    /// A group can be considered holding the geometric content of an object.
    /// </summary>
    [DataContract]
    public class GeometryElementGroup3D
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GeometryElementGroup3D" /> class.
        /// </summary>
        /// <param name="elements">element list</param>
        public GeometryElementGroup3D(List<GeometryElement3D> elements)
        {
            this.ElementGroup = elements;
        }

        /// <summary>
        /// Gets a list of geometry elements
        /// </summary>
        [DataMember]
        public List<GeometryElement3D> ElementGroup { get; private set; }

        /// <summary>
        /// Compute the distances from geometry elements of another element group
        /// represented as the shortest distance (represented as a positive number)
        /// between them or the largest penetration depth (represented in a negative number).
        /// </summary>
        /// <param name="elementGroup">An element group</param>
        /// <returns>Distance between two element groups.  When it is positive, it indicates
        /// the shortest distance between the two groups.  When it is negative, it represents
        /// the largest penetration depth.</returns>
        public double DistanceFrom(GeometryElementGroup3D elementGroup)
        {
            List<GeometryElement3D> elementGroup1 = this.ElementGroup;
            List<GeometryElement3D> elementGroup2 = elementGroup.ElementGroup;
            double distance = double.MaxValue;

            for (int i = 0; i < elementGroup1.Count; ++i)
            {
                for (int j = 0; j < elementGroup2.Count; ++j)
                {
                    double value = elementGroup1[i].DistanceFrom(elementGroup2[j]);
                    distance = Math.Min(value, distance);
                }
            }

            return distance;
        }
    }
}
